/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.charge.dao.IBusinessCodeDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.service.IParameterDefService;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.charge.vo.ParameterDefVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.dao.IPassChargeItemConfigDao;
import com.ce.pms.system.renovation.model.PassChargeItemConfigDO;
import com.ce.pms.system.renovation.service.IPassChargeItemConfigService;
import com.ce.pms.system.renovation.vo.PassChargeItemConfigVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：装修出入证收费项目<br>
 * @author 易贤建 创建日期：2008-12-25<br>
 */
public class PassChargeItemConfigServiceImpl extends PmsBaseServiceImpl implements
		IPassChargeItemConfigService {
	/**
	 * 交易码server
	 */
	private IBusinessCodeService  businessCodeService= null;
	/**
	 * 装修出入证收费项目DAO
	 */
	private IPassChargeItemConfigDao passChargeItemConfigDao = null;

	/**
	 * 费用参数定义表
	 */
	private IParameterDefService parameterDefService = null;

	public final String DEPOSIT_CODE = "DEPOSIT_CODE"; //押金
	public final String PASS_COST_CODE = "PASS_COST_CODE"; //工本费交易码

	/**
	 * 功能描述:根据ID获取VO<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	public PassChargeItemConfigVO findById(Long keyword){
		checkParameterIsNull(keyword,"system:approval_info_id_not_null"); //请选择一条记录
		PassChargeItemConfigDO configDO =  passChargeItemConfigDao.findById(keyword);
		return assembleD2V(configDO);
	}
	/**
	 * 功能描述:查询所有的装修出入证收费项目<br>
	 * @param searchVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	@SuppressWarnings("unchecked")
	public Page queryPassChargeItemConfigList(SearchVO searchVO){
		Page page = passChargeItemConfigDao.queryPassChargeItemConfigList(searchVO);
		List result = assembleD2V(page.getResult());
		page.setResult(result);
		return page;
	}
	/**
	 * 功能描述:添加装修出入证收费项目<br>
	 * @param searchVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	public PassChargeItemConfigVO add(PassChargeItemConfigVO configVO){
		checkPassChargeItemConfigInfo(configVO);

		PassChargeItemConfigDO configDO =  assembleV2D(configVO);
		configDO.setSysInfo(super.createInfo(configVO.getCreator()));
		passChargeItemConfigDao.saveObject(configDO);
		return assembleD2V(configDO);
	}
	/**
	 * 功能描述:修改装修出入证收费项目<br>
	 * @param searchVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	public PassChargeItemConfigVO edit(PassChargeItemConfigVO configVO){
		checkPassChargeItemConfigInfo(configVO);
		checkParameterIsNull(configVO.getKeyword(),"system:approval_info_id_not_null"); //请选择一条记录

		PassChargeItemConfigDO configDO = passChargeItemConfigDao.findById(configVO.getKeyword());
		checkParameterIsNull(configDO,"system:approval_info_object_not_found"); //系统没找到对应的记录
		configDO.setDescription(configVO.getDescription()); //设置描述
		configDO.setPrice(configVO.getPrice()); //收费标准
		//入账方式
		configDO.setEnterAccountMethod(configVO.getEnterAccountMethod());
		configDO.setState(configVO.getState()); //使用状态
		configDO.setSysInfo(super.createInfo(configVO.getModifier()));
		return assembleD2V(configDO);
	}
	/**
	 * 功能描述:删除对应的记录<br>
	 * @param keyword
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	public void delete(Long keyword){
		checkParameterIsNull(keyword,"system:approval_info_id_not_null"); //请选择一条记录
		PassChargeItemConfigDO configDO = passChargeItemConfigDao.findById(keyword);
		checkParameterIsNull(configDO,"system:approval_info_object_not_found"); //系统没找到对应的记录
		passChargeItemConfigDao.deleteObject(configDO);
	}
	/**
	 * 功能描述:获取出入证指定的交易码<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public List<BusinessCodeVO> getBusinessCodeList(){
		ParameterDefVO parameter_deposit = parameterDefService.findParameterDefByPkey(this.DEPOSIT_CODE);//获取押金
		if(parameter_deposit == null || parameter_deposit.getPvalue() == null)
			throw new BusinessException("system:renovate_charge_error",BusinessException.ERROR);
		BusinessCodeVO depositCodeVO = businessCodeService.findBusinessCodeByKeyword(parameter_deposit.getPvalue());
		List<BusinessCodeVO> incomeBizCode = businessCodeService.getAllIncomeBusinessCode();
		incomeBizCode.add(depositCodeVO);
		return incomeBizCode;
	}
	/**
	 * 功能描述:验证项目名称唯一<br>
	 * @param configVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public void validateItemName(PassChargeItemConfigVO configVO){
		if(configVO == null) return ;
		if(BlankUtil.isBlank(configVO.getItemName())) return ;

		Long keyword = configVO.getKeyword();
		String itemName = configVO.getItemName();

		List<PassChargeItemConfigDO> appDOS = null;
		if(keyword == null){
			appDOS = passChargeItemConfigDao.findByItemName(itemName);
		}else{
			appDOS = passChargeItemConfigDao.findByItemNameWithoutKeyword(keyword,itemName);
		}
		if(!BlankUtil.isBlank(appDOS))
			throw new BusinessException("system:passChargeItem_itemName_isExist",BusinessException.WARNING);
	}
	/**
	 * 功能描述: 检查基本信息<br>
	 * @param configVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private void checkPassChargeItemConfigInfo(PassChargeItemConfigVO configVO){
		checkParameterIsNull(configVO, "system:approval_info_not_null"); //信息不能为空
		checkParameterIsNull(configVO.getPrice(),"system:approval_price_not_null"); //收费标准不能为空
	}

	private PassChargeItemConfigDO assembleV2D(PassChargeItemConfigVO configVO){
		if(configVO == null) return null;

		PassChargeItemConfigDO configDO = new PassChargeItemConfigDO();
			configDO.setKeyword(configVO.getKeyword()); //主键
			processBizCodeV2D(configDO,configVO); //交易码
			configDO.setDescription(configVO.getDescription()); //描述
			configDO.setItemName(configVO.getItemName()); //收费项目名称
			configDO.setPrice(configVO.getPrice()); //收费标准
			configDO.setEnterAccountMethod(configVO.getEnterAccountMethod());
			configDO.setState(configVO.getState()); //使用状态
		return configDO;
	}
	/**
	 * 功能描述: 将VO中的交易码初始化到DO<br>
	 * @param configDO
	 * @param configVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private void processBizCodeV2D(PassChargeItemConfigDO configDO,PassChargeItemConfigVO configVO){
		BusinessCodeDomain businessCode = new BusinessCodeDomain();
		businessCode.setKeyword(configVO.getBizKeyword());
		configDO.setBusinessCode(businessCode);
	}
	/**
	 * 功能描述:将LIST DO TO VO<br>
	 * @param configDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private List<PassChargeItemConfigVO> assembleD2V(List<PassChargeItemConfigDO> configDOS){
		if(BlankUtil.isBlank(configDOS)) return null;

		List<PassChargeItemConfigVO> configVOS = new ArrayList<PassChargeItemConfigVO>();
		for(PassChargeItemConfigDO configDO : configDOS){
			configVOS.add(assembleD2V(configDO));
		}
		return configVOS;
	}
	/**
	 * 功能描述:将单个DO TO VO<br>
	 * @param configDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private PassChargeItemConfigVO assembleD2V(PassChargeItemConfigDO configDO){
		if(configDO == null) return null;

		PassChargeItemConfigVO configVO = new PassChargeItemConfigVO();
		configVO.setKeyword(configDO.getKeyword()); //主键
		configVO.setBizKeyword(configDO.getBusinessCode().getKeyword()); //交易码主键
		configVO.setBizCode(configDO.getBusinessCode().getBizCode()); //交易码
		configVO.setDescription(configDO.getBusinessCode().getDescription()); //描述
		configVO.setItemName(configDO.getItemName()); //收费项目名称
		configVO.setPrice(configDO.getPrice()); //收费标准
		//入账方式
		configVO.setEnterAccountMethod(configDO.getEnterAccountMethod());
		configVO.setState(configDO.getState()); //使用状态

		return configVO;
	}
	public void setPassChargeItemConfigDao(
			IPassChargeItemConfigDao passChargeItemConfigDao) {
		this.passChargeItemConfigDao = passChargeItemConfigDao;
	}

	public void setParameterDefService(IParameterDefService parameterDefService) {
		this.parameterDefService = parameterDefService;
	}
	public void setBusinessCodeService(IBusinessCodeService businessCodeService) {
		this.businessCodeService = businessCodeService;
	}
}
